#include "runara.h"
#include <cglm/types-struct.h>
#include <stdio.h>
#include <unistd.h> // For file existence check (critical for font path validation)

#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <glad/glad.h>

RnState *state;

void resizecb(GLFWwindow *window, int w, int h) {
  if (state) {
    rn_resize_display(state, w, h);
  }
  glViewport(0, 0, w, h);
}

int main() {
  if (!glfwInit()) {
    fprintf(stderr, "ERROR: Failed to initialize GLFW\n");
    return -1;
  }

  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);

  GLFWwindow *window = glfwCreateWindow(800, 600, "Runara Example", NULL, NULL);
  if (!window) {
    fprintf(stderr, "ERROR: Failed to create GLFW window\n");
    glfwTerminate();
    return -1;
  }
  glfwMakeContextCurrent(window);

  if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
    fprintf(stderr, "ERROR: Failed to initialize Glad\n");
    glfwDestroyWindow(window);
    glfwTerminate();
    return -1;
  }

  glfwSetFramebufferSizeCallback(window, resizecb);
  int initial_width, initial_height;
  glfwGetFramebufferSize(window, &initial_width, &initial_height);
  resizecb(window, initial_width, initial_height);

  state =
      rn_init(initial_width, initial_height, (RnGLLoader)glfwGetProcAddress);
  if (!state) {
    fprintf(stderr, "ERROR: Failed to initialize Runara\n");
    glfwDestroyWindow(window);
    glfwTerminate();
    return -1;
  }

  // --------------------------
  // Fix 1: Validate font path first (debug silent path errors)
  // --------------------------
  const char *font_path = "/usr/share/fonts/truetype/freefont/FreeSans.ttf";
  if (access(font_path, R_OK) != 0) { // Check if file exists and is readable
    fprintf(stderr, "ERROR: Font file not found/readable: %s\n", font_path);
    fprintf(stderr,
            "Try: sudo apt install fonts-freefont-ttf (to install FreeSans)\n");
    return -1;
  }

  // Load font (keep your original API)
  RnFont *font = rn_load_font(state, font_path, 24);

  // --------------------------
  // Fix 2: Don't set font=NULL (Runara may need valid pointer for fallback)
  // --------------------------
  if (!font || font->id == 0) {
    fprintf(stderr, "WARNING: Failed to load font (using Runara fallback)\n");
    // Keep font pointer (don't set to NULL) – many libs need non-NULL for
    // fallback
  }

  while (!glfwWindowShouldClose(window)) {
    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    rn_begin(state);

    // --------------------------
    // Fix 3: Opaque color (0xFFFFFFFF = ARGB white, not 0xFFFFFF)
    // --------------------------
    vec2s text_pos =
        (vec2s){50.0f, 100.0f}; // Slight Y-adjust for better visibility
    vec2s render_size =
        (vec2s){(float)state->render.render_w, (float)state->render.render_h};
    RnColor text_color = rn_color_from_hex(
        0xFFFFFFFF); // Add full alpha (critical for visibility)

    if (font && font->id != 0) {
      // RnTextProps rn_text_render(RnState * state, const char *text,
      //                            RnFont *font, vec2s pos, RnColor color)
      // Draw with loaded font (your original API)
      rn_text_render(state, "Hello Runara!", font, text_pos, text_color);
    } else {
      // Draw fallback (keep your original API – use font pointer, not NULL)
      rn_text_render(state, "Hello (Fallback Font)!", font, text_pos,
                     text_color); // Yellow (0xFFFFFF00 = ARGB yellow)
    }

    rn_end(state);

    glfwSwapBuffers(window);
    glfwPollEvents();
  }

  // Cleanup (keep your original API)
  if (font) {
    rn_free_font(state, font);
  }
  rn_terminate(state);
  glfwDestroyWindow(window);
  glfwTerminate();

  return 0;
}